package vn.com.vega.music.view.adapter;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.AlbumDetail.ISelectSongPlay;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.music.objects.Album;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumdetailAdapter extends BaseAdapter {
	private static int ALBUM_DETAIL_TYPE = 0;
	public static int HEADER_TYPE = 1;
//	private static int ROW_SONG_TYPE = 2;
	private static int NUMBER_TYPE = 2;

	private Context mContext;
	private LayoutInflater inflater;
	private Album mAlbum;
	private ISelectSongPlay selectSongPlay;
	
	public void setSelectSongPlayInterface(ISelectSongPlay interfaceSelect) {
		selectSongPlay = interfaceSelect;
	}

	public AlbumdetailAdapter(Context context, Album album) {
		mContext = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAlbum = album;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 2 /*+ mAlbum.listSong.size()*/;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
//		if (position >= 2) {
//			return mAlbum.listSong.get(position);
//		} else {
			return null;
//		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int viewType = getItemViewType(position);
		AlbumDetailHolder holder;
		if (viewType == ALBUM_DETAIL_TYPE) {
//			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.album_detail_item_artist, null);
				holder = new AlbumDetailHolder();
				holder.albumThumb = (ImageView) convertView
						.findViewById(R.id.imv_album_thumb);
				holder.albumName = (TextView) convertView
						.findViewById(R.id.tv_album_name);
				holder.albumArtist = (TextView) convertView
						.findViewById(R.id.tv_album_artist);
//				convertView.setTag(holder);
//			} else {
//				holder = (AlbumDetailHolder) convertView.getTag();
//			}
			Appinstant app = (Appinstant) mContext.getApplicationContext();
			app.mImageLoader.download(mAlbum.coverUrl, holder.albumThumb);
			holder.albumName.setText(mAlbum.title);
			holder.albumArtist.setText(mAlbum.artistName);
			return convertView;

		} else /*if (viewType == HEADER_TYPE)*/ {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.header_list_song, null);
				holder = new AlbumDetailHolder();
				holder.playAllSong = (TextView) convertView
						.findViewById(R.id.tv_play_all_song);
				holder.selectSongPlay = (TextView) convertView
						.findViewById(R.id.tv_select_song_play);
				holder.playAllSong.setOnClickListener(playAllClickListener);
				holder.selectSongPlay.setOnClickListener(selectSongPlayListener);
//				convertView.setTag(holder);
			} /*else {
				holder = (AlbumDetailHolder) convertView.getTag();
			}*/
			return convertView;
		} /*else {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.song_item, null);
				holder = new AlbumDetailHolder();
				holder.songName = (TextView) convertView
						.findViewById(R.id.tv_song_title);
				holder.songListenCount = (TextView) convertView
						.findViewById(R.id.tv_count_song_listen);
				holder.songArtistName = (TextView) convertView
						.findViewById(R.id.tv_artist_name);
				convertView.setTag(holder);
			} else {
				holder = (AlbumDetailHolder) convertView.getTag();
			}
			Song song = (Song) mAlbum.listSong.get(position - ROW_SONG_TYPE);
			holder.songArtistName.setText(song.artist_name);
			holder.songName.setText(song.title);
			holder.songListenCount.setText(song.viewCount + "");
			return convertView;
		}*/
	}
	
	View.OnClickListener playAllClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (selectSongPlay != null) {
				selectSongPlay.playAllSong();
			}
		}
	};
	
	View.OnClickListener selectSongPlayListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (selectSongPlay != null) {
				selectSongPlay.selectSongPlay();
			}
		}
	};

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (position == 0) {
			return ALBUM_DETAIL_TYPE;
		} else /*if (position == 1) */{
			return HEADER_TYPE;
		} /*else
			return ROW_SONG_TYPE;*/
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return NUMBER_TYPE;
	}

	static class AlbumDetailHolder {
		ImageView albumThumb;
		TextView albumName;
		TextView albumArtist;
		TextView songArtistName;
		TextView songName;
		TextView songListenCount;
		TextView playAllSong;
		TextView selectSongPlay;
	}

}
